source('codes/00_import_libraries.R')


#----------------------------------------------------------------------------#
# Specifications -------------------------------------------------------------
#----------------------------------------------------------------------------#

PLS = 'PLS_All'
dict_name = 'base'

topic_list = readRDS('input/narrative_dicts_v2.RDS')[[dict_name]] %>% names()
topic_list = c(topic_list, PLS)
topic_names = c('War', 'Pandemic', 'Panic','Confidence',
                'Savings','Conspicuous Consumption',
                'Monetary Standard','Technology Replacing Jobs',
                'Real Estate Booms', 'Real Estate Crashes', 
                'Stock Market Bubbles', 'Stock Market Crashes',
                'Boycotts and Evil Business', 'Wage and Labor Unions',
                # 'Future',
                'Topic PLS')


#----------------------------------------------------------------------------#
# Data -------------------------------------------------------------
#----------------------------------------------------------------------------#

dt = readRDS('input/combined_data.rds')[['base']] %>%
  select(ym, mkt0, all_of(topic_list)) %>%
  mutate(
    ym = as.Date(as.yearmon(ym)),
    mkt = mkt0 * 100,
    mkt = mkt - mean(mkt, na.rm = TRUE),
    
    across(all_of(topic_list), ~ (. - mean(., na.rm = TRUE)) * 100),
    across(all_of(PLS), ~ ./10)
  ) %>% 
  filter(as.yearmon(ym) >= 'Jan 1871')



#---------------------#
# Recession 
#---------------------#

recess = read_excel('input/us recession dummy.xlsx') %>% 
  mutate(
    date = as.Date(as.yearmon(ym)),
    xmin = date,
    xmax = lead(date),
    ymin = -100,
    ymax = 100
  ) %>% 
  filter(as.yearmon(date) >= 'Jan 1871')



#----------------------------------------------------------------------------#
# Function -------------------------------------------------------------------
#----------------------------------------------------------------------------#

plot_topic = function(i, line_size, text_size) {
  
  dt$var = dt[[topic_list[i]]]
  
  gplot = ggplot(data = dt, aes(x = ym, y = var)) + 
    
    geom_rect(
      data = recess, aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax, fill = as.factor(recess)), 
      inherit.aes = FALSE, alpha = 1
    ) +
    scale_fill_manual(values = c('white', 'grey')) +
    
    geom_line(size = line_size) +
    
    labs(title = topic_names[i], y = '', x = '') +
    coord_cartesian(
      expand = FALSE, # this is important
      xlim   = as.Date(c('1870-01-01', '2019-12-31')),
      ylim   = c(-20, 20),
      ) +
    
    scale_y_continuous(breaks = pretty_breaks()) +
    scale_x_date(date_breaks = '10 year', date_labels = '%Y') +
    
    theme_base() +
    theme(
      text                = element_text(size = text_size),
      legend.position     = 'none',
      plot.title          = element_text(hjust = 0.5),
      plot.margin         = unit(c(0,0,-0.5,0), 'pt'),
      plot.background     = element_rect(colour = NA)
   )
  return(gplot)
}



#----------------------------------------------------------------------------#
# plot -----------------------------------------------------------------------
#----------------------------------------------------------------------------#

#---------------------#
# Individual plots 
#---------------------#

# for (i in seq_along(topic_list)) {
#   p = plot_topic(i = i, line_size = 0.8, text_size = 20)
  
#   ggsave(
#     paste0('output/figures/Figure_3/ts_', topic_list[i], '.pdf'),
#     plot = p,
#     height = 5, width = 11
#   )
# }


#---------------------#
# Group plots 
#---------------------#

gplots = lapply(seq_along(topic_list), function(i) {
  plot_topic(i = i, line_size = 0.3, text_size = 15)
})
names(gplots) = topic_list


plot1 = grid.arrange(
  gplots$PLS_All, gplots$War, gplots$Pandemic, 
  gplots$Panic, gplots$Money, gplots$Real_estate_boom,
  gplots$Boycott, ncol = 2
)
ggsave('output/figures/Figure_3.tiff', plot1, width = 15, height = 18, device='tiff', dpi=300)


plot2 = grid.arrange(
  gplots$Confidence, gplots$Saving, gplots$Consumption, 
  gplots$Tech, gplots$Real_estate_crash, gplots$Stock_bubble,
  gplots$Stock_crash, gplots$Wage, ncol = 2
)










